What is readable-web-to-node-stream?
The readable-web-to-node-stream package provides a way to convert web streams (ReadableStream) into Node.js streams (Readable). This is particularly useful for working with web APIs in a Node.js environment, allowing seamless integration between web and Node.js stream ecosystems.
What are readable-web-to-node-stream's main functionalities?
Convert Web ReadableStream to Node.js Readable Stream
This feature allows you to convert a web ReadableStream obtained from a fetch request into a Node.js Readable stream. The code sample demonstrates fetching data from a URL and converting the response body into a Node.js stream, which can then be used with Node.js stream APIs.
const { ReadableWebToNodeStream } = require('readable-web-to-node-stream');
async function fetchAndConvert(url) {
const response = await fetch(url);
const webStream = response.body;
const nodeStream = new ReadableWebToNodeStream(webStream);
nodeStream.on('data', chunk => {
console.log('Received chunk:', chunk);
});
}
fetchAndConvert('https://example.com');
Other packages similar to readable-web-to-node-stream
web-streams-polyfill
The web-streams-polyfill package provides a polyfill for the Web Streams API, making it available in environments where it is not natively supported. Unlike readable-web-to-node-stream, it focuses on providing the Web Streams API itself rather than converting between web and Node.js streams.
node-fetch
The node-fetch package is a lightweight module that brings `window.fetch` to Node.js. While it does not directly convert web streams to Node.js streams, it allows you to use the Fetch API in Node.js, which can be used in conjunction with readable-web-to-node-stream for stream conversion.
stream-web
The stream-web package provides utilities for working with web streams in Node.js. It includes functions for converting between web streams and Node.js streams, similar to readable-web-to-node-stream, but offers a broader set of utilities for stream manipulation.
readable-web-to-node-stream
Converts a Web-API readable stream into a Node.js readable stream.
Installation
Install via npm:
npm install readable-web-to-node-stream
or or yarn:
yarn add readable-web-to-node-stream
Compatibility
Source is written in TypeScript and compiled to ECMAScript 2017 (ES8).
Unit tests are performed on the following browsers:
- Google Chrome 74.0
- Firefox 68.0
- Safari 12.0
- Opera 60.0
Example
Import readable-web-stream-to-node in JavaScript:
const {ReadableWebToNodeStream} = require('readable-web-to-node-stream');
async function download(url) {
const response = await fetch(url);
const readableWebStream = response.body;
const nodeStream = new ReadableWebToNodeStream(readableWebStream);
}
API
constructor(stream: ReadableStream): Promise
stream: ReadableStream
: the Web-API readable stream.
close(): Promise
Will cancel close the Readable-node stream, and will release Web-API-readable-stream.
waitForReadToComplete(): Promise
If there is no unresolved read call to Web-API ReadableStream immediately returns, otherwise it will wait until the read is resolved.
Licence
(The MIT License)
Copyright (c) 2019 Borewit
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.